# """
# This is Master's API interface.
# You should not implement it, or speculate about its implementation
# """
class Master:
    def __init__(self, secret=None):
        self.secret = secret

    def guess(self, word: str) -> int:
        return Solution.cnt_same(word, self.secret)


from typing import List
from itertools import combinations
from collections import defaultdict

class Solution:
    @staticmethod
    def cnt_same(w1, w2):
        return sum(c1 == c2 for c1, c2 in zip(w1, w2))

    def findSecretWord(self, words: List[str], master: 'Master') -> None:
        maps = defaultdict(lambda: defaultdict(list))
        for w1, w2 in combinations(words, 2):
            cnt = self.cnt_same(w1, w2)
            maps[w1][cnt].append(w2)
            maps[w2][cnt].append(w1)
        # print(maps)
        
        def get_score(m):
            lst = [len(m[i]) for i in range(6)]
            ave = sum(lst) / len(lst)
            return sum([abs(i - ave) for i in lst])

        scores = {w: get_score(maps[w]) for w in maps}
        possible = [(w, s) for w, s in scores.items()]
        time = 0
        while len(possible) > 0:
            g, score = min(possible, key=lambda x: x[1])
            res = master.guess(g)
            time += 1
            if res == 6:
                return
            np = maps[g][res]
            possible = [(w, s) for w, s in possible if w in np]
            # 可以迭代maps

if __name__ == '__main__':
    s = Solution()
    words = ["wichbx","oahwep","tpulot","eqznzs","vvmplb","eywinm","dqefpt","kmjmxr","ihkovg","trbzyb","xqulhc","bcsbfw","rwzslk","abpjhw","mpubps","viyzbc","kodlta","ckfzjh","phuepp","rokoro","nxcwmo","awvqlr","uooeon","hhfuzz","sajxgr","oxgaix","fnugyu","lkxwru","mhtrvb","xxonmg","tqxlbr","euxtzg","tjwvad","uslult","rtjosi","hsygda","vyuica","mbnagm","uinqur","pikenp","szgupv","qpxmsw","vunxdn","jahhfn","kmbeok","biywow","yvgwho","hwzodo","loffxk","xavzqd","vwzpfe","uairjw","itufkt","kaklud","jjinfa","kqbttl","zocgux","ucwjig","meesxb","uysfyc","kdfvtw","vizxrv","rpbdjh","wynohw","lhqxvx","kaadty","dxxwut","vjtskm","yrdswc","byzjxm","jeomdc","saevda","himevi","ydltnu","wrrpoc","khuopg","ooxarg","vcvfry","thaawc","bssybb","ccoyyo","ajcwbj","arwfnl","nafmtm","xoaumd","vbejda","kaefne","swcrkh","reeyhj","vmcwaf","chxitv","qkwjna","vklpkp","xfnayl","ktgmfn","xrmzzm","fgtuki","zcffuv","srxuus","pydgmq"]
    s.findSecretWord(words, Master("ccoyyo"))
